/*
 * Matrix.cpp
 *
 *  Created on: Feb 15, 2011
 *      Author: alki
 */

#include "Matrix.h"
#include <iostream>

using namespace std;

Matrix::Matrix() {
	a_11=a_12=a_13=0;
	a_21=a_22=a_23=0;
	a_31=a_32=a_33=0;
}

Matrix::~Matrix() {
}

Vektor Matrix::operator *(const Vektor &v) {
	Vektor neu(this->a_11*v.getX()
			+this->a_12*v.getY()
			+this->a_13*v.getZ(),
			this->a_21*v.getX()
			+this->a_22*v.getY()
			+this->a_23*v.getZ(),
			this->a_31*v.getX()
			+this->a_32*v.getY()
			+this->a_33*v.getZ());
	return neu;
}

Matrix Matrix::operator *(const Matrix &m) {
	Matrix neu;
	neu.a_11=this->a_11*m.a_11+this->a_12*m.a_21+this->a_13*m.a_31;
	neu.a_12=this->a_11*m.a_12+this->a_12*m.a_22+this->a_13*m.a_32;
	neu.a_13=this->a_11*m.a_13+this->a_12*m.a_23+this->a_13*m.a_33;

	neu.a_21=this->a_21*m.a_11+this->a_22*m.a_21+this->a_23*m.a_31;
	neu.a_22=this->a_21*m.a_12+this->a_22*m.a_22+this->a_23*m.a_32;
	neu.a_23=this->a_21*m.a_13+this->a_22*m.a_23+this->a_23*m.a_33;

	neu.a_31=this->a_31*m.a_11+this->a_32*m.a_21+this->a_33*m.a_31;
	neu.a_32=this->a_31*m.a_12+this->a_32*m.a_22+this->a_33*m.a_32;
	neu.a_33=this->a_31*m.a_13+this->a_32*m.a_23+this->a_33*m.a_33;
	return neu;
}

Matrix Matrix::operator *(float f) {
	Matrix neu;
	neu.a_11=this->a_11*f;
	neu.a_12=this->a_12*f;
	neu.a_13=this->a_13*f;
	neu.a_21=this->a_21*f;
	neu.a_22=this->a_22*f;
	neu.a_23=this->a_23*f;
	neu.a_31=this->a_31*f;
	neu.a_32=this->a_32*f;
	neu.a_33=this->a_33*f;
	return neu;
}

float Matrix::Det() {
	return
			a_11*a_22*a_33+
			a_12*a_23*a_31+
			a_13*a_21*a_32-
			a_11*a_23*a_32-
			a_12*a_21*a_33-
			a_13*a_22*a_31;
}

Matrix Matrix::Inv() {
	Matrix inv;
	float det=this->Det();
	inv.a_11=a_22*a_33-a_23*a_32;
	inv.a_12=a_13*a_32-a_12*a_33;
	inv.a_13=a_12*a_23-a_13*a_22;

	inv.a_21=a_23*a_31-a_21*a_33;
	inv.a_22=a_11*a_33-a_13*a_31;
	inv.a_23=a_13*a_21-a_11*a_23;

	inv.a_31=a_21*a_32-a_22*a_31;
	inv.a_32=a_12*a_31-a_11*a_32;
	inv.a_33=a_11*a_22-a_12*a_21;

	inv = inv/det;
	return inv;
}

void Matrix::out() {
	cout << a_11 << " , " << a_12 << " , " << a_13 << endl;
	cout << a_21 << " , " << a_22 << " , " << a_23 << endl;
	cout << a_31 << " , " << a_32 << " , " << a_33 << endl;
}
